package etc.com.market;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Handler;

public class CheckingNetworkThread extends Thread 
{
	private Context ctx;
	private Runnable myRunnable;
	public static int count=0;
	public CheckingNetworkThread(Context ctx,Runnable r)
	{
		super("Test_Network");
		this.ctx = ctx;
		this.myRunnable = r;
		
	}
	@Override
	public void run()
	{
		boolean isConnection = true;
		while(isConnection)
		{
			try{
				
				synchronized (this)
				{
					isConnection =  chkStatus();
				}
					if(isConnection == false)
					{
//						new CheckingNetworkThread(ctx, myRunnable).start();
						myHandler.post(myRunnable);
							
//					}else
//					{
						
						
//					}
					}
					
					Thread.sleep(1000);
				
			   }catch(Exception e)
			   {
				   
				   break;
			   }
		}
	}

	private boolean chkStatus()
	 {
		 ConnectivityManager connMgr = (ConnectivityManager)
		 ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
		 
		 final android.net.NetworkInfo wifi =
		 connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		 
		 final android.net.NetworkInfo mobile =
		 connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
		 
		 if( wifi.isConnected() ){
		 //Toast.makeText(this, "Wifi" , Toast.LENGTH_LONG).show();
		 return true;
		 }
		 else if( mobile.isConnected() ){
		 //Toast.makeText(this, "Mobile 3G " , Toast.LENGTH_LONG).show();
		 return true;
		 }else
		 {
			// Toast.makeText(this, "No Network! Please check internet connection. " , Toast.LENGTH_SHORT).show();
			 return false;
		 }
	
	 }
	private Handler myHandler = new Handler(); 
//	private Runnable myRunnable = new Runnable() {
//		
//		public void run() {
//			// TODO Auto-generated method stub
//			//Toast.makeText(MapActivity.this, "wth", Toast.LENGTH_LONG).show();
////			imgSearch.setText("dsfsdfsdfsf");
//			synchronized(this)
//			{
//				AlertDialog.Builder alt_bld = new AlertDialog.Builder(ctx);
//	        	alt_bld.setMessage("Do you want to close this window ?")
//	        	.setCancelable(false)
//	        	.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
//	        	public void onClick(DialogInterface dialog, int id) {
//	        	// Action for 'Yes' Button
////	        		Intent in = new Intent(MapActivity.this,MainMenu.class);
////	        		startActivity(in);
//	        	}
//	        	})
//	        	.setNegativeButton("No", new DialogInterface.OnClickListener() {
//	        	public void onClick(DialogInterface dialog, int id) {
//	        	//  Action for 'NO' Button
//	        	dialog.cancel();
//	        	}
//	        	});
//	        	AlertDialog alert = alt_bld.create();
//	        	// Title for AlertDialog
//	        	alert.setTitle("Title");
//	        	// Icon for AlertDialog
//	        	alert.setIcon(R.drawable.icon);
//	        	alert.show();
//			}
//		}
//	};
}
